#include "game.h"

//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
    int i = 0;
    int j = 0;
    for(i=0; i<rows; i++)
    {
        for(j=0; j<cols; j++)
        {
         board[i][j] = set;   
        }
        
    }
}

//打印
void Display(char board[ROWS][COLS], int row, int col)
{
    int i = 1;
    int j = 1;
    printf("*****扫雷游戏*****\n");
    for(j=0; j<=col; j++)
    {
        printf("%d ",j);
    }
    printf("\n");
    for(i=1; i<=row; i++)
    {
        printf("%d ",i);
        for(j=1; j<=col; j++)
        {
            printf("%c ",board[i][j]);
        }
        printf("\n");
    }
}

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
    int count = EASY_COUNT;
    while(count)
    {
        int x = rand() % row + 1; //0~8 --1-9
        int y = rand() % col + 1;
        if(board[x][y] == '0')
        {
          board[x][y] = '1';  
          count --;
        } 
    }
}

//排查雷
int get_mine_count(char board[ROWS][COLS], int x, int y)
{
    //x,y周围的格子相加
    //字符不能直接相加，减去字符'0'
    return (board[x-1][y-1] +
            board[x-1][y] +
            board[x-1][y+1] +
            board[x][y-1] + 
            board[x][y+1] +
            board[x+1][y-1] +
            board[x+1][y] +
            board[x+1][y+1] - 8 * '0');
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    int x = 0;
    int y = 0;
    int win = 0;//找到非雷的个数
    while(win < row*col-EASY_COUNT)
    {
        printf("请输入要排查的坐标:>");
        scanf("%d %d", &x, &y);
        if(x>=1 && x<=9 && y>=1 && y<=9)
        {
            if(show[x][y] != '*')
            {
                printf("该坐标已被排查过了，请重新输入\n");
                continue;
            }
            if(mine[x][y] == '1')
            {
                //踩到雷
                printf("很遗憾，你被炸死了\n");
                Display(mine, ROW, COL);
                break;
            }
            else{
                //不是雷，排查周围8个格子
                win++;
                int count = get_mine_count(mine, x, y);//统计mine数组中x,y坐标周围有几个雷
                show[x][y] = count + '0';//转换为数字字符
                Display(show, ROW, COL);
            }
        }
        else{
            printf("输入不合法，请重新输入\n");
        }
    }
    if(win == row*col - EASY_COUNT)
    {
        printf("恭喜你赢了\n");
        Display(show, ROW, COL);
    }
}
